package main

import (
	"fmt"
	"time"
)

func sayHello() {

	for i := range 5 {
		fmt.Println("hello ", i)
		time.Sleep(100 * time.Millisecond)
	}
}

func sayWorld() {
	for i := range 5 {
		fmt.Println("world ", i)
		time.Sleep(100 * time.Millisecond)
	}
}

func sendData(ch chan string) {
	time.Sleep(3 * time.Second)
	ch <- "hello from goroutine"
}

func main() {
	go sayHello()
	go sayWorld()
	time.Sleep(1 * time.Second)

	ch := make(chan string)
	go sendData(ch)

	msg := <-ch
	fmt.Println("收到消息: ", msg)

	fmt.Println("main 结束")

	ch1 := make(chan int, 3) // 容量为 3

	ch1 <- 1
	ch1 <- 2
	ch1 <- 3

	fmt.Println(<-ch1) // 输出 1
	fmt.Println(<-ch1) // 输出 2
	fmt.Println(<-ch1) // 输出 3

	test2()
}
